package com.example.tse.rbyair.PKmy;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.example.tse.rbyair.PKmy.fragment.OverdueCouponFragment;
import com.example.tse.rbyair.PKmy.fragment.UnusedCouponFragment;
import com.example.tse.rbyair.PKmy.fragment.UsedCouponFragment;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.base.RbBaseActivity;
import com.example.tse.rbyair.login.adaper.MainFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class CouponActivity extends RbBaseActivity implements OnClickListener {
	private ImageView mTabLine;
	private ViewPager mViewPager;
	private MainFragmentPagerAdapter adapter;
	private List<Fragment> fragmentList;
	private int currentIndex;
	private int screenWidth;
	
	public static boolean isForeground = false;
	UnusedCouponFragment unusedCouponFragment;
	UsedCouponFragment usedCouponFragment;
	OverdueCouponFragment overdueCouponFragment;
//	EmptyCouponFragment emptyCouponFragment;
	private TextView unused;
	private TextView used;
	private TextView overdue;
	private LinearLayout unusedLayout;
	private LinearLayout usedLayout;
	private LinearLayout overdueLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coupon_main);
		initView();
		initTabLine();

	}
	
	private void initView() {

		setLeftTxt(R.string.mine);
		setTitleTxt(R.string.coupon);
		hideRightImage();

		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
		unused = (TextView) findViewById(R.id.unusedTxt);
		used = (TextView) findViewById(R.id.usedTxt);
		overdue = (TextView) findViewById(R.id.overdueTxt);

		unusedLayout = (LinearLayout)findViewById(R.id.unusedLayout);
		usedLayout = (LinearLayout)findViewById(R.id.usedLayout);
		overdueLayout = (LinearLayout)findViewById(R.id.overdueLayout);

		unusedLayout.setOnClickListener(this);
		usedLayout.setOnClickListener(this);
		overdueLayout.setOnClickListener(this);

		fragmentList = new ArrayList<Fragment>();

		unusedCouponFragment = new UnusedCouponFragment();
		usedCouponFragment = new UsedCouponFragment();
		overdueCouponFragment = new OverdueCouponFragment();
//		emptyCouponFragment = new EmptyCouponFragment();

		fragmentList.clear();
		
		fragmentList.add(unusedCouponFragment);
		fragmentList.add(usedCouponFragment);
		fragmentList.add(overdueCouponFragment);
//		fragmentList.add(emptyCouponFragment);

		adapter = new MainFragmentPagerAdapter(this.getSupportFragmentManager(), fragmentList);

		mViewPager.setAdapter(adapter);

		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				resetTextView();
				switch (position) {
				case 0:
					unused.setTextColor(getResources().getColor(
							R.color.login_background));
					break;
				case 1:
					used.setTextColor(getResources().getColor(
							R.color.login_background));
					break;
				case 2:
					overdue.setTextColor(getResources().getColor(
							R.color.login_background));
					break;
				}
				currentIndex = position;
			}

			// 设置3个界面的显示位置
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {

				LayoutParams lp = (LayoutParams) mTabLine
						.getLayoutParams();
				if (currentIndex == 0 && position == 0) {

					lp.leftMargin = (int) (positionOffset
							* (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));

				} else if (currentIndex == 1 && position == 0) {// 1-->0
					lp.leftMargin = (int) (-(1 - positionOffset)
							* (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));

				} else if (currentIndex == 1 && position == 1) {// 1-->2
					lp.leftMargin = (int) (positionOffset
							* (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));

				} else if (currentIndex == 2 && position == 1) {// 2-->1
					lp.leftMargin = (int) (-(1 - positionOffset)
							* (screenWidth * 1.0 / 3) + currentIndex
							* (screenWidth / 3));

				}
				mTabLine.setLayoutParams(lp);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		// 设置
		mViewPager.setCurrentItem(0);
	}

	private void initTabLine() {
		mTabLine = (ImageView) findViewById(R.id.id_tab_line);

		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay()
				.getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		LayoutParams lp = (LayoutParams) mTabLine
				.getLayoutParams();
		lp.width = screenWidth / 3;
		mTabLine.setLayoutParams(lp);
	}

	private void resetTextView() {
		unused.setTextColor(getResources().getColor(R.color.line));
		used.setTextColor(getResources().getColor(
				R.color.line));
		overdue.setTextColor(getResources().getColor(R.color.line));
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.unusedLayout:
			mViewPager.setCurrentItem(0);
			break;

		case R.id.usedLayout:
			mViewPager.setCurrentItem(1);
			break;

		case R.id.overdueLayout:
			mViewPager.setCurrentItem(2);
			break;
		}
	}

}






